package com.qc31.monitorlib.ui.listmonitor

import android.os.Handler
import android.os.HandlerThread
import android.os.Looper
import android.os.Message
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import autodispose2.autoDispose
import com.qc31.baselib.auto.AutoDisposeViewModel
import com.qc31.baselib.utils.ArrayUtil
import com.qc31.baselib.utils.CacheMMKV
import com.qc31.comlib.UserConstant
import com.qc31.comlib.entity.GpsDetail
import com.qc31.comlib.net.ServiceHelper
import com.qc31.monitorlib.R

/** @Author Jasper @Time 2020/11/12 @Desc 列表监控 */
class ListMonitorViewModel : AutoDisposeViewModel() {

    private val locations = ArrayList<GpsDetail>()
    private val runCars = ArrayList<GpsDetail>()
    private val stopCars = ArrayList<GpsDetail>()
    private val offlineCars = ArrayList<GpsDetail>()
    private val arrearsCars = ArrayList<GpsDetail>()
    private val alarmCars = ArrayList<GpsDetail>()
    private val locationCars = ArrayList<GpsDetail>()


    private val gpsLive = MutableLiveData<List<ArrayList<GpsDetail>>>()
    val gpsObserver: LiveData<List<ArrayList<GpsDetail>>> = gpsLive
    private val gpsList = ArrayList<ArrayList<GpsDetail>>()


    //车队ID
    var teamId = ""

    var isClear = false

    private var mCarsHandle: UpdateHandle? = null
    private val mCarsHandleThread: HandlerThread by lazy { HandlerThread("mListHandleThread") }


    init {
        initThread()
    }

    /** 初始化线程 */
    private fun initThread() {
        mCarsHandleThread.start()
        mCarsHandle = UpdateHandle(mCarsHandleThread.looper)
    }

    /** 停止线程 */
    private fun stopThread() {
        mCarsHandle?.removeCallbacksAndMessages(null)
        mCarsHandleThread.quit()
    }

    var sortByType: String
        get() = CacheMMKV.decodeString("sort_type", "carPlate")
        set(value) {
            CacheMMKV.encode("sort_type", value)
            refreshData()
        }

    private fun refreshData() {
        if (locations.size > 0) {
            val msg = Message.obtain()
            msg.what = 1
            mCarsHandle?.sendMessage(msg)
        }
    }


    /** 查询定位数据 */
    fun getCarsLocation() {
        if (isClear || UserConstant.current_car_num > 300 && teamId.isEmpty()) {
            toast(R.string.toast_choose_team)
            return
        }
        toastLoading(true)
        ServiceHelper.getRepository().getGpsRInfo(teamId, "", simple = false, filter = false)
            .autoDispose(this)
            .subscribe({
                toastLoading(false)
                val msg = Message.obtain()
                msg.obj = it
                msg.what = 1
                mCarsHandle?.sendMessage(msg)
                locations.clear()
                locations.addAll(it)
            }, error)
    }


    internal inner class UpdateHandle(looper: Looper) : Handler(looper) {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            if (msg.what == 0) {
                runCars.clear()
                stopCars.clear()
                offlineCars.clear()
                alarmCars.clear()
                arrearsCars.clear()
                locationCars.clear()

                for (entity in locations) {
                    when (entity.state) {
                        "1" -> {
                            offlineCars.add(entity)
                        }
                        "2" -> {
                            offlineCars.add(entity)
                            arrearsCars.add(entity)
                        }
                        "3" -> {
                            offlineCars.add(entity)
                        }
                        "4" -> {
                            offlineCars.add(entity)
                            alarmCars.add(entity)
                        }
                        "13" -> {
                            locationCars.add(entity)
                        }
                        "6" -> {
                            arrearsCars.add(entity)
                        }
                        "7" -> {
                            runCars.add(entity)
                        }
                        "8" -> {
                            runCars.add(entity)
                            alarmCars.add(entity)
                        }
                        "9", "10" -> {
                            stopCars.add(entity)
                        }
                        "11", "12" -> {
                            alarmCars.add(entity)
                            stopCars.add(entity)
                        }
                    }

                }
                gpsList.clear()
                gpsList.add(locations)
                gpsList.add(offlineCars)
                gpsList.add(runCars)
                gpsList.add(stopCars)
                gpsList.add(alarmCars)
                gpsList.add(arrearsCars)
                gpsList.add(locationCars)
                gpsLive.postValue(gpsList)

            } else {
                val locationsList = ArrayUtil.sortBy(locations, sortByType, true)
                val msg = Message.obtain()
                msg.obj = locationsList
                msg.what = 0
                mCarsHandle?.sendMessage(msg)

            }
        }
    }

    override fun onCleared() {
        super.onCleared()
        stopThread()
    }
}
